#include "myUSART.h"

#define OMGBAUDRATE 115200

bufferQue bufferCOM[3];


/* 
    DIPAKAI
    JPT
    0    USART 2  A2  A3
    1    UART 4   A0  A1
    HMI
        USART1   C6  C7
*/

void initALL_USART(void){
    bMakeEmpty(&bufferCOM[0]);
    bMakeEmpty(&bufferCOM[1]);
    bMakeEmpty(&bufferCOM[2]);
    
    initUSART2();
    initUART4();
    initUSART6();
   
}

void initUSART6(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /*Change Here*/
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART6, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        
    /*Change Here*/
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);


    /*Change Here*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    
    /*Change Here*/
    GPIO_Init(GPIOC, &GPIO_InitStructure);    
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);


    USART_InitStructure.USART_BaudRate = OMGBAUDRATE;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    
    /*Change Here*/
    USART_Init(USART6, &USART_InitStructure);
    

    /*Change Here*/
    USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); 
    NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
    
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
    NVIC_Init(&NVIC_InitStructure);				
    
    /*Change Here*/
    USART_Cmd(USART6, ENABLE);
}



void initUSART2(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /*Change Here*/
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        
    /*Change Here*/
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);


    /*Change Here*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    
    /*Change Here*/
    GPIO_Init(GPIOA, &GPIO_InitStructure);    
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);


    USART_InitStructure.USART_BaudRate = OMGBAUDRATE;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    
    /*Change Here*/
    USART_Init(USART2, &USART_InitStructure);
    

    /*Change Here*/
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); 
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
    NVIC_Init(&NVIC_InitStructure);				
    
    /*Change Here*/
    USART_Cmd(USART2, ENABLE);
}

void initUART4(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    /*Change Here*/
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        
    /*Change Here*/
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);


    /*Change Here*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    
    /*Change Here*/
    GPIO_Init(GPIOA, &GPIO_InitStructure);    
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);


    USART_InitStructure.USART_BaudRate = OMGBAUDRATE;  
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    
    /*Change Here*/
    USART_Init(UART4, &USART_InitStructure);
    

    /*Change Here*/
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); 
    NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
    
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
    NVIC_Init(&NVIC_InitStructure);				
    
    /*Change Here*/
    USART_Cmd(UART4, ENABLE);
}




void USART_putchar(USART_TypeDef* USARTx, int ch)
{
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
    
    USART_SendData(USARTx, (uint8_t)ch);
    
    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);
    
}


void USART_puts(USART_TypeDef* USARTx, volatile char *s){

	while(*s){ 
		USART_putchar(USARTx, *s);
		*s++;
	}

    //while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);
}


void UART4_IRQHandler(void){
    if( USART_GetITStatus(UART4, USART_IT_RXNE) ){
		char t = UART4->DR;
        bEnque(&bufferCOM[1], t);
    }
}

void USART2_IRQHandler(void){
    if( USART_GetITStatus(USART2, USART_IT_RXNE) ){
		char t = USART2->DR;
        bEnque(&bufferCOM[0], t);
    }
}


void USART6_IRQHandler(void){
    if( USART_GetITStatus(USART6, USART_IT_RXNE) ){
		char t = USART6->DR;
        bEnque(&bufferCOM[2], t);
    }
}


